<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultiamte
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * </pre>
 * 
 * Created on	:  Sep 24, 2008
 * 
 * @author		hoadn <hoadn@rubik.com.vn>
 *
 */

defined ( '_FW_EXEC' ) or die ( 'Restricted access' );
RCoreImporter::cls('html.AbstractFieldsControl');

/**
 * Class for generation the fields inside a form
 *
 */
class FormFieldsControl extends AbstractFieldsControl {
	
	var $_jsValidate = true;
	var $_frmHeader = 'Information form';
	var $_expectedArrange = null;
	var $_totalCol = 2;
	
	/**
	 * Set the expected arrange of the form
	 * Ex of input:
	 * 		array('user_name', array('first_name', 'last_name'), 'birthday', array('address', 'city', 'country'));
	 *
	 * @param array $arrExpectedArrange this array show the expected order on each row.
	 */
	function setExpectedArrange($arrExpectedArrange) {
		$this->_expectedArrange = $arrExpectedArrange;
		// Calculate the total column
		foreach ($arrExpectedArrange as $arrOrKey) {
			$num = 2;
			if (is_array($arrOrKey)) {
				$num = count($arrOrKey);
				$num = $num * 2;
			}
			$this->_totalCol = max($this->_totalCol, $num);
		}
	}
	
	/**
	 * Build Field HTML
	 *
	 * @param  AbstractRubikDTO $dto
	 * @return string
	 */
	function _buildField($dto) {
		$dto = $this->_dto;
		
		$dbInfo = RubikDBFactory::getDBInfo ( $dto );
		$allFields = $this->_arrayFields;
		
		if (!$this->_expectedArrange) {
			$this->_expectedArrange = array();
		foreach ( $allFields as $key => $fieldRule ) {
				$this->_expectedArrange[] = $key;
			}
		}
		
		$return = '';
		$visibleFields = array();
		// For each row
		foreach ( $this->_expectedArrange as $arrOrKey ) {
			$return .="\n\t<tr>";
			
			if (is_array($arrOrKey)) {
				$count = count($arrOrKey);
				$numOfCells = intval($this->_totalCol / $count);
				
				for($i=0; $i < $count-1; $i++) {
					$key = $arrOrKey[$i];
					$return .= $this->_createFieldHtml($key, $dbInfo, $dto, $numOfCells);
				}
				$key = $arrOrKey[$i];
				$return .= $this->_createFieldHtml($key, $dbInfo, $dto, $this->_totalCol - $numOfCells*$i);
			}
			else {
				$return .= $this->_createFieldHtml($arrOrKey, $dbInfo, $dto, $this->_totalCol);
			}
			
			$return .="\n\t</tr>";
		}
		return $return;
	}
	
	function _createFieldHtml($key, $dbInfo, $dto, $numOfCells) {
		$fieldRule = $this->_arrayFields[$key];
			$fieldInfo = $dbInfo->getDbField ( $key );
			$lbl_field = $this->getLabel ( $key );
			$fieldName = $key;
			$fieldData = $dto->$key;
			
			$requireSym = '';
			$requireCls = null;
			if ($fieldInfo->isMandatory) {
				$requireSym = ' <span class="required">*</span>';
				$requireCls = 'required';
			}
			
			if ($fieldRule->isHtmlFieldInfo) {
				if ($fieldInfo) {
					$fieldRule->setValue($fieldData);
				}
				$fieldRule->setValidateCssCls($requireCls, '_req');
				$fieldData = $fieldRule->getHtmlInputData($fieldInfo->dataType);
			
			$colSpan = $numOfCells;
			if ($fieldRule->showLabel()) {
				$colSpan--;
				$return .= "\n\t\t<td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>";
			}
			$return .= "\n\t\t<td class=\"form-input\" colspan=\"{$colSpan}\">{$fieldData}</td>";
			} else if ($fieldRule->_isColumnInfo) {
			$colSpan--;
				$fieldRule->setPrefixToName ( $prefix );
				$fieldData = $fieldRule->generateHTML ( $fieldData );
			$return .= "\n\t\t<td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>";
			$return .= "\n\t\t<td class=\"form-input\" colspan=\"{$colSpan}\">{$fieldData}</td>";
			} else {
				if ($fieldInfo->isKey) {
					$fieldData = htmlspecialchars($fieldData);
					$return .= "<input name=\"{$fieldName}\" type=\"hidden\" value=\"{$fieldData}\" id=\"{$fieldName}\" />";
				return $return;
				}
				else {
				$colSpan--;
				$return .= "<td class=\"form-lable\"><label for=\"{$fieldName}\">{$lbl_field}{$requireSym}</label></td>";
				if (empty($fieldRule)) {
					$fieldData = htmlspecialchars($fieldData);
					$return .= "<td class=\"form-input\" colspan=\"{$colSpan}\"><input name=\"{$fieldName}\" value=\"{$fieldData}\" id=\"{$fieldName}\" /></td>";
				}
				else {
					$return .= "<td class=\"form-input\" colspan=\"{$colSpan}\">{$fieldRule}</td>";
				}
			}
		}
		return $return;
	}
	
	/**
	 * Set the text for the header table.
	 *
	 * @param string $text if the text is empty --> there isn't any header
	 */
	function setHeaderText($text) {
		$this->_frmHeader = $text;
	}
	
	/**
	 * Build the HTML data for all fields
	 *
	 * @return string
	 */
	function _buildData() {
		$return = "<table id=\"contenttext_{$this->_inputName}\" class=\"tblControll\">";
		if ($this->_frmHeader) {
			$return .= '<tr><th colspan="'.$this->_totalCol.'">'.$this->_frmHeader.'</th></tr>';
		}
		$return .= $this->_buildField ( $this->_dto );
		$return .= "</table>";
		return $return;
	}
	
	/**
	 * Enabale or disable js database
	 *
	 * @param boolean $validate
	 */
	function setJsValidate($validate) {
		$this->_jsValidate = $validate;
	}
	
	/**
	 * Generate the HTML
	 *
	 * @return string
	 */
	function generateHtml() {
		if ($this->_jsValidate) {
			SysResponse::addJavascriptFile('core/js/jquery.validate.min.js');
			
			$js = '
				$(document).ready(function(){
					if ($.validator) {
						$("#systemForm").validate();
					}
				});
			';
			SysResponse::addJavascriptContent($js);
		}
		
		$result = '';
		$result .= '<div class="wrapper">';
		$result .= $this->_buildData ();
		$result .= '</div>';
		return $result;
	}
	
	/**
	 * Print the html
	 *
	 */
	function printHtml() {
		echo $this->generateHtml();
	}
}
?>
